import axios from 'axios'
import store from '../store'

var baseURL

const instance = axios.create({
    baseURL,
    // timeout: 5000
})

instance.interceptors.request.use(
    function (config) {
        //在请求前要做的
        console.log(store.state);
        if (store.state.token) {
            config.headers.Authorization = 'Bearer' + store.state.token

        }

        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    function (response) {

        //在相应是做些什么

        return response
    },
    function (error) {
        return Promise.reject(error)
    }
)

export default function http(url, method, data = {}, params = {}) {
    return instance({
        url, method, data, params
    })
        .then((res) => {
            return res
        })
        .catch(err => {
            return Promise.reject(err)
        })
}

